var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
//引入swagger
const swaggerUi = require('swagger-ui-express');
//swagger 接口注释 内容，
const swaggerDoc = require('./config/swagger.json');


var indexRouter = require('./routes/index');
var usersRouter = require('./routes/userslogin');
var userlogin = require('./moudles/userslogin/routes/users');
var roomRouter = require('./routes/roomVue');
var orderRouter=require('./routes/orderOB');
var app = express();

//设置允许跨域访问该服务.
app.all('*', function (req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    res.header('Access-Control-Allow-Methods', '*');
    res.header('Content-Type', 'application/json;charset=utf-8');
    next();
});


// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
//使用setup 函数把注释转json ,swaggerUi 把json 转swagger 网页界面
app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerDoc));
//它是HTTP请求日志中间件 可以在控制台上记录HTTP请求的详细信息，比如请求方法、路径、状态码等。
// logger('dev')是morgan的一个预定义选项，它设置日志格式为开发环境中常见的格式，包括请求方法、路径、响应状态码、响应时间等信息。
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

//在express 上面挂载路由
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/', userlogin)
app.use('/findAllRooms', roomRouter)
app.use('/', roomRouter)
app.use('/queryorder',orderRouter)
app.use('/buyroom',orderRouter)
//错误处理


// catch 404 and forward to error handler
app.use(function (req, res, next) {
    next(createError(404));
});


// error handler
app.use(function (err, req, res, next) {
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};

    // render the error page
    res.status(err.status || 500);
    res.render('error');
});

module.exports = app;
